import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import state from './state'
import mutations from './mutations'
import actions from './actions'
import getters from './getters'


Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 使用 webStorage 进行存储，刷新网页后用户信息仍存在
    isLogin: sessionStorage.getItem("isLogin"),
    userId: sessionStorage.getItem("userId"),
    addressInfoNow: sessionStorage.getItem("addressInfoNow") || { title: "暂时无法获取定位" },

    addressTitle: sessionStorage.getItem("addressTitle") || "暂时无法获取定位",  //只保存店铺名称
    addressDetail: sessionStorage.getItem("addressDetail") || "暂无对应地址",  //保存店铺详情地址

    ProductData: [], //所有商品的数据
    totalCount: 0, //选择商品总数
    totalPrice: 0, //选择商品总价格
    selectedFood: [], //选择购买的商品
    // selectedFood: sessionStorage.getItem("selectedFood") || [],

    count: 0,  //当前商品的个数

    // 第一步 vuex 声明变量
    orderDetailId:null,
  },


  mutations: {
    setOrderId(state,orderDetailId){
      state.orderDetailId = orderDetailId;
    },
    // setIsLogin(){
    //   isLogin=state.isLogin
    // },
    // 修改用户登录状态以及当前的用户id
    setIsLogin(state, userId) {
      state.isLogin = true;
      state.userId = userId;
    },
    // 修改当前地址信息
    setAddressInfoNow(state, addressInfoNow) {
      state.addressInfoNow = addressInfoNow;
    },

    // 修改当前店铺名称
    setAddressTitle(state, addressTitle) {
      state.addressTitle = addressTitle
    },
    // 修改当前店铺地址
    setAddressDetail(state, addressDetail) {
      state.addressDetail = addressDetail;
    },

    // 更新所有商品数据的函数
    setProduct(state, list) {
      state.ProductData = list;
    },

    // 点击加减号，更改数量与总价
    SET_TOTAL(state, payload) {
      // 传进来的参数有多个，用对象接收
      const {
        type,
        shopId,
        price,
      } = payload;
      switch (type) {
        case 'add': //当点击的为加号时，价格和数量均增加
          state.totalPrice += price;
          state.totalCount += 1;
          state.count += 1;
          console.log(`用户id:${state.userId},当前商品价格：${price},当前商品数量：${state.count}`);
          break;
        case 'minus':
          if (state.totalCount > 0) {
            state.totalPrice -= price;
            state.totalCount -= 1;
            state.count -= 1;
          }
          break;
        default:
          break;
      }
    },

    // 购物车商品信息
    SET_CART(state, payload) {
      const {
        type,
        shopId,
        img,
        price,
        scId,
        slogan,
        name,
        // count,
      } = payload;
      // 看购物车中是否有这个商品
      const index = state.selectedFood.findIndex(item => Number(item.shopId) === shopId);
      if (index === -1) { // 当购物车中没有这个商品时,向数组中加入这个商品
        state.selectedFood.push({
          type,
          shopId,
          img,
          price,
          scId,
          slogan,
          name,
          count: 1,
          totalCount: 1,
          totalPrice: price
        })
      } else { //如果有这个商品，根据点击的按钮(加号还是减号)对数量进行更改
        switch (type) {
          case 'add':
            state.selectedFood[index].count += 1;
            state.selectedFood[index].totalCount += 1;
            state.selectedFood[index].totalPrice += price;
            break;
          case 'minus':
            if (state.totalCount) {
              state.selectedFood[index].count -= 1;
              state.selectedFood[index].totalCount -= 1;
              state.selectedFood[index].totalPrice -= price;
            }
            break;
          default:
            break;
        }
      }
    }
  },


  actions: {
    // 请求所有商品数据并替换 state 中的值
    getProductData(context) {
      axios.get("/api/shop/getShopAll", {
        params: {
          pageNum: 1,
          pageSize: 100
        }
      })
        .then(res => {
          context.commit("setProduct", res.data.data.list);
        });
    },

    setTotal({
      commit
    }, payload) {
      commit('SET_TOTAL', payload);
    },
    setCart({
      commit
    }, payload) {
      commit('SET_CART', payload)
    }
  },
  modules: {},
  state,
  mutations,
  actions,
  getters
})